/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.business.service.impl;

import me.zhengjie.modules.business.domain.UserFlagDO;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.business.repository.UserFlagRepository;
import me.zhengjie.modules.business.service.UserFlagService;
import me.zhengjie.modules.business.service.dto.UserFlagDto;
import me.zhengjie.modules.business.service.dto.UserFlagQueryCriteria;
import me.zhengjie.modules.business.service.mapstruct.UserFlagMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author ljj
* @date 2021-02-25
**/
@Service
@RequiredArgsConstructor
public class UserFlagServiceImpl implements UserFlagService {

    private final UserFlagRepository userFlagRepository;
    private final UserFlagMapper userFlagMapper;

    @Override
    public Map<String,Object> queryAll(UserFlagQueryCriteria criteria, Pageable pageable){
        Page<UserFlagDO> page = userFlagRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(userFlagMapper::toDto));
    }

    @Override
    public List<UserFlagDto> queryAll(UserFlagQueryCriteria criteria){
        return userFlagMapper.toDto(userFlagRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public UserFlagDto findById(Integer id) {
        UserFlagDO userFlag = userFlagRepository.findById(id).orElseGet(UserFlagDO::new);
        ValidationUtil.isNull(userFlag.getId(),"UserFlag","id",id);
        return userFlagMapper.toDto(userFlag);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserFlagDto create(UserFlagDO resources) {
        return userFlagMapper.toDto(userFlagRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UserFlagDO resources) {
        UserFlagDO userFlag = userFlagRepository.findById(resources.getId()).orElseGet(UserFlagDO::new);
        ValidationUtil.isNull( userFlag.getId(),"UserFlag","id",resources.getId());
        userFlag.copy(resources);
        userFlagRepository.save(userFlag);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            userFlagRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<UserFlagDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserFlagDto userFlag : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("机器码", userFlag.getDeviceId());
            map.put("唯一识别码", userFlag.getIdentityId());
            map.put("应用key", userFlag.getAppKey());
            map.put("实验key", userFlag.getExperimentKey());
            map.put("版本值", userFlag.getVariableValue());
            map.put("创建时间", userFlag.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}